# -*- coding:utf8 -*-

"""
    Copyright 2010 Yurtaev Egor

    This file is part of webapp-dcpp-log.

    webapp-dcpp-log is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    webapp-dcpp-log is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with webapp-dcpp-log. If not, see http://www.gnu.org/licenses/.
"""

import json, replase

class makeJSON():
    def __init__(self, db_web):
        self.db_web = db_web
    def msg(self, id):
        dic = []
        query = self.db_web.query("SELECT rowid, date, time, nick, message FROM GlobalChat WHERE rowid > " + str(id))
        for i in query:
            temp = {
                "rowid":i["rowid"],
                "date":i["date"],
                "time":i["time"],
                "nick":i["nick"],
                "message":replase.all(i["message"])
            }
            dic.append(temp)
        return json.dumps({"data": dic}, ensure_ascii = False, indent = 4, separators = (',', ':'))

    def userlist(self):
        dic = []
        query = self.db_web.query("SELECT List FROM UserList")
        dic = query[0]['List'].split("$$")
        return json.dumps({"data":dic[:-1]}, ensure_ascii = False, indent = 4, separators = (',', ':'))

    def idLastMsg(self):
        id = self.db_web.query("SELECT rowid FROM GlobalChat ORDER BY rowid DESC LIMIT 1")[0]['rowid']
        return json.dumps({"data":[id]}, ensure_ascii = False, indent = 4, separators = (',', ':'))